package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Depositbusiness;

import java.util.List;

/**
 * @desrciption 存款业务dao层
 * @auth：yidu 李小锋
 * @date:2021/3/13 0013 - 8:59
 * @version:1:0
 */
@Repository//标识为Spring中的Bean
public interface DepositbusinessDao {
    /**
     * 新增一行数据
     * @param depositbusiness 对象
     * @return 0、增加失败 1、增加成功
     */
    @Insert("insert into depositBusiness(Id,caId,caIds,depMoney,businType,expiryDate,handIf,incl,businTime,dofa,fundId)" +
            "values" +
            "(seqDepositBusiness.nextval,#{caId},#{caIds},#{depMoney},#{businType},#{expiryDate},#{handIf},#{incl},#{businTime},#{dofa},#{fundId})")
    public int add(Depositbusiness depositbusiness);

    /**
     * 删除一行数据
     * @param deposiId 编号
     * @return 0、删除失败 1、删除成功
     */
    @Delete("delete from depositBusiness where Id=#{deposiId}")
    public int delete(@Param("deposiId") int deposiId);

    /**
     * 修改一行数据
     * @param depositbusiness 对象
     * @return 0、修改失败 1、修改成功
     */
    @Update("update depositBusiness set caId=#{caId},caIds=#{caIds},depMoney=#{depMoney}," +
            "businType=#{businType},expiryDate=#{expiryDate},handIf=#{handIf},incl=#{incl}," +
            "businTime=#{businTime},dofa=#{dofa},fundId=#{fundId} where Id=#{deposiId}")
    public int update(Depositbusiness depositbusiness);
    /**
     * 查询所有的订单数据
     * @return 返回一个订单集合
     */
    @Select("select * from  depositBusiness")
    @Results(id = "depositMap",value = {
            @Result(id = true,property = "deposiId",column = "Id"),
            @Result(property = "caId",column = "caId"),
            @Result(property = "caIds",column = "caIds"),
            @Result(property = "depMoney",column = "depMoney"),
            @Result(property = "businType",column = "businType"),
            @Result(property = "expiryDate",column = "expiryDate"),
            @Result(property = "handIf",column = "handIf"),
            @Result(property = "incl",column = "incl"),
            @Result(property = "businTime",column = "businTime"),
            @Result(property = "dofa",column = "dofa"),
            @Result(property = "fundId",column = "fundId")
    })
    public List<Depositbusiness> findAll();
    /**
     * 分页查询
     * @param start  开始行号
     * @param rows   每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @ResultMap(value = "depositMap")
    @Select("<script>" +
            "select * from (select rownum rn,depositBusiness.* from depositBusiness" +
            "<where> #{rows}>rownum </where>)" +
            "<where>" +
            " ${condition}"+
            " and rn>=#{start}" +
            "</where>" +
            "</script>"
    )
    public List<Depositbusiness> findByPage(@Param("start") int start, @Param("rows") int rows, @Param("condition") String condition);

    /**
     * 按条件统计出记录行数
     * @param condition 条件
     * @return 记录行数
     */
    @Select("select count(*) from depositBusiness where 1=1 ${condition}")
    public int count(@Param("condition") String condition);

    /**
     * 通过id查询存款数据
     * @param id
     * @return
     */
    @Select("select * from depositBusiness where Id=#{id}")
    public Depositbusiness queryDep(@Param("id") String id);

    /**
     * 存款业务添加现金应收应付数据
     */
    @Insert("insert into cashReceivable(Id,businDate,caId,businType,capFlow,money,fundId,states) " +
            "values(seqCashReceivable.nextval,sysdate,#{caId},#{businType},#{capFlow},#{money},#{fundId},#{states})")
    public void addCashs(@Param("caId") String caId,@Param("businType") String businType,@Param("capFlow") int capFlow,
                         @Param("money") double money,@Param("fundId") String fundId,@Param("states") int states);

    /**
     * 存款业务添加到现金应收应付之后将状态改为已处理
     * @param id
     */
    @Update("update depositBusiness set handIf=1 where Id=#{id}")
    public void updateState(String id);

    /**
     * 添加资金调拨
     */
    @Insert("insert into fundAllocation(tofId,fundId,caId,reType,reMoney,businDate,reDate,mark,dial) " +
            "values(seqFundAllocation.nextval,#{fundId},#{caId},#{reType},#{money},sysdate,sysdate,'未处理',#{capFlow})")
    public void addDep(@Param("fundId") String fundId,@Param("caId") String caId,@Param("reType") String reType,@Param("money") double money,@Param("capFlow") int capFlow);

    /**
     * 通过流入账户取得卡号利率
     * @param caIds
     * @return
     */
    @Select("select cardRate from cashAccount where caId=#{caIds}")
    public Double getCardRate(@Param("caIds") int caIds);

    /**
     * 取出该存款应收应付的利息
     * @return
     */
    @Select("select nvl(sum(money),0) from cashReceivable where caId=#{caIds} and businType='存款利息' and money=#{cardRate}")
    public Double getDueMoney(@Param("caIds") String caIds,@Param("cardRate") Double cardRate);

    /**
     * 删除应收应付
     * @param caIds
     * @param cardRate
     * @return
     */
    @Delete("delete from cashReceivable where caId=#{caIds} and businType='存款利息' and money=#{cardRate}")
    public int deleteDueMoney(@Param("caIds") String caIds,@Param("cardRate") Double cardRate);
}
